package com.after.saas.user.controller;

import com.after.saas.user.service.UserService;
import com.after.saas.common.annotation.Log;
import com.after.saas.common.annotation.NoRepeatSubmit;
import com.after.saas.common.annotation.RequireAuthorize;
import com.after.saas.common.api.service.ExternUserService;
import com.after.saas.common.constants.Result;
import com.after.saas.common.model.bo.UserBO;
import com.after.saas.common.model.query.CommonQuery;
import com.after.saas.common.model.query.UserQuery;
import com.after.saas.common.model.vo.UserVO;
import com.after.saas.common.utils.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private ExternUserService externUserService;

    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
    public Result getUserInfo() {
        return ResponseUtil.SUCCESS(userUtil.getUserInfo());
    }

    @ApiOperation("获取单个用户")
    @GetMapping(value = "/findOne")
    public Result findOne(UserQuery query) {
        return ResponseUtil.SUCCESS(userService.findOne(query));
    }

    @ApiOperation("获取用户列表（分页）")
    @PostMapping(value = "/page")
    public Result<Page<UserVO>> queryByPage(@RequestBody UserQuery query) {
        return ResponseUtil.SUCCESS(userService.queryByPage(query));
    }

    @RequireAuthorize(hasAnyPerm = {"user:query", "user:*"})
    @ApiOperation("获取用户列表（不分页）")
    @PostMapping(value = "/list")
    public Result<List<UserVO>> list(@Valid @RequestBody UserQuery query) {
        return ResponseUtil.SUCCESS(userService.queryAll(query));
    }

    @ApiOperation("根据id获取获取用户")
    @GetMapping(value = "/findById")
    public Result<UserVO> findById(Long id) {
        return ResponseUtil.SUCCESS(userService.findById(id));
    }

    @ApiOperation("获取前端所需菜单")
    @GetMapping(value = "/buildMenus")
    public Result buildMenus() {
        UserVO userVO = userUtil.getUserInfo();
        return ResponseUtil.SUCCESS(userVO);
    }

    @Log(value = "注册用户")
    @NoRepeatSubmit(timeout = 5)
    @PostMapping("/registry")
    public Result<UserVO> registry(@Valid @RequestBody UserVO userVO) {
        return ResponseUtil.SUCCESS(externUserService.registry(userVO));
    }

    @Log(value = "批量导入用户")
    @PostMapping("/batchSave")
    public Result<Boolean> batchSave(@RequestBody List<UserBO> boList) {
        return ResponseUtil.SUCCESS(userService.batchSave(boList));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, CommonQuery query) {
        List<UserBO> userBOS = ListUtil.copyList(userService.list(), UserBO.class);
        EasyExcelUtil.export(response, userBOS, UserBO.class, "测试导出用户");
    }

    @NoRepeatSubmit
    @GetMapping("/test")
    public Result show(UserQuery query) {
        log.warn("query {}", JSON.toJSONString(query));
        return ResponseUtil.SUCCESS();
    }

}
